package com.cefa.edu.controller;

import com.cefa.edu.common.result.FailureResult;
import com.cefa.edu.common.result.Result;
import com.cefa.edu.common.result.ReturnMessage;
import com.cefa.edu.common.result.SuccessResult;
import com.cefa.edu.model.*;
import com.cefa.edu.service.*;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController {
    private Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    IndexService indexService;

    @Autowired
    LoginService loginService;
    @Autowired
    UserService userService;
    @Autowired
    EducationLevelService educationLevelService;



//    /**
//     * 添加层级
//     * @param educationLevel
//     * @param userId
//     * @param roleType
//     * @param names 如果是添加的教育阶段，会自动生成教育阶段下的年级信息，其他情况可以为空
//     * @return
//     */
//    @RequestMapping(path="/education/levelSave", method = RequestMethod.POST)
//    @CrossOrigin
//    public Result<Map<String,Object>> levelSave(EducationLevel educationLevel ,Integer userId,Integer roleType,String names) {
//        Map<String, Object> result = Maps.newHashMap();
//        boolean falt=true;
//        try {
//            falt= validateLevelSave(educationLevel.getLevel(),educationLevel.getName());
//            if(falt){
//                educationLevelService.insertEducationLevel(educationLevel,userId,roleType,names);
//                result.put("falt",true);
//            }else{
//                result.put("falt",false);
//            }
//        }catch (Exception e){
//            logger.error(e.getMessage());
//            result.put("falt",false);
//        }
//        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
//    }
    public boolean validateLevelSave(Integer level,String name){
        boolean falt=true;
        if(level==1){
            int count=educationLevelService.getEducationLevelByLevelAndName(level,name);
            if(count>0){
                falt=false;
            }
        }
        return falt;
    }
    /**
     * 层级删除(包括教育阶段、年级、班级)
     * @param levelId  层级id
     * @param level 层级 1、2、3
     * @return
     */
    @RequestMapping(path="/education/deleteLevel", method = RequestMethod.POST)
    @CrossOrigin
    public Result<Map<String,Object>> deleteLevel(Integer levelId,Integer level) {
        boolean falt=false;
        Map<String, Object> result = Maps.newHashMap();
        try {
            falt=validateLevel(levelId,level);
            if(falt){
                indexService.deleteDucationLevel(levelId,level);
            }
            result.put("falt",falt);
        }catch (Exception e){
            logger.error(e.getMessage());
            result.put("falt",false);
            return new FailureResult<>(new ReturnMessage("500", e.getMessage()), result);
        }
        return new SuccessResult<>(new ReturnMessage("200", "success"), result);
    }

    public boolean validateLevel(Integer levelId,Integer level){
        boolean falt=true;
        //1、根据id查询cefa_edu_education_level表里p_id=id ，如果有则不许删除，如果没有（即没有子类），直接删除即可
        if(level==1){
            List<EducationLevel> list=indexService.getEducationLevelByGid(levelId);//查询是否有班级，现在年级是不让删的
            if(list!=null&&list.size()>0){
                falt=false;
            }
        }else if(level==3){
            //如果是班级，则需要查询班级下是否有老师，是否有学生，如果有则不能删，都没有，直接删除即可
            int count=userService.getUserCount(levelId);
            if(count>0){
                falt=false;
            }
        }else{//只有1层和3层可以删除
            falt=false;
        }
        return falt;
    }


}
